package com.sinopec.session;

import java.util.logging.Level;

import com.crystaldecisions.sdk.occa.infostore.*;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.framework.CrystalEnterprise;
import com.crystaldecisions.sdk.exception.SDKException;
import com.crystaldecisions.sdk.occa.pluginmgr.IPluginInfo;
import com.crystaldecisions.sdk.occa.pluginmgr.IPluginMgr;
import com.crystaldecisions.sdk.plugin.desktop.user.IUser;
import com.crystaldecisions.sdk.plugin.desktop.usergroup.IUserGroup;

public class BoUser {
	//all user added in this group
	private static final String reportGroup = "ReportGroup";
	private String name;
	private String password;
	
	public BoUser(String name,String password){
		this.name = name;
		this.password = password;
	}
	public BoUser(){}
	
	//password
	public String getPassword() {
		return this.password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	//name
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	//log on to cms and return a enterPriseSession 
	public IEnterpriseSession logon(String cms){
		IEnterpriseSession boEnterpriseSession = null;
		String boAuthType = "secEnterprise";
		try {
			boEnterpriseSession = CrystalEnterprise.getSessionMgr().
			logon(this.name,this.password,cms,boAuthType);
		}
		catch(SDKException e){
			//return null;
			throw new RuntimeException(e.getMessage());
		}
		return boEnterpriseSession;
	}
	

	//get ReportGroup id
	private int getReportGroupId(IInfoStore boInfoStore,String groupName){
		IUserGroup boGroup = null;
		try{
			boGroup = (IUserGroup)boInfoStore.query("SELECT * FROM CI_SYSTEMOBJS " +
				"WHERE SI_PROGID='CrystalEnterprise.USERGROUP' AND SI_NAME='" + groupName 
				+ "'").get(0);
		}
		catch(SDKException e){
			throw new RuntimeException(e.getMessage());
			//return false;
		}
		return boGroup.getID();
		
	}
	/** create a new user and add to ReportGroup
	 * user name: bi_account + "_" + info_repo.user.id
	 * password = "our" + info_repo.user.password + "BI"
	   * @param email:user email 
	   * @param bi_account: bi_account in info_repo table
	   * @param userID: user id in info_repo table
	   * @param pwd:user input password 
	   * @return return true if create a new user else false
	   * @exception SQLException
	  */
	public boolean CreateUser(String sellerID,String sellerNick,
							String pwd,String cms){
		IPluginMgr boPluginMgr = null; //Plugin Manager returned from InfoStore
	   	IPluginInfo boUserPlugin = null; //User PluginInfo object
	   	IInfoObjects boNewInfoObjects = null; //new collection created from InfoStore
	   	IInfoObject boNewUser = null; //New User created
	   	
	   	IUser boUserInfo = null; //IUser interface to allow us to change user specific settings
	   	IInfoStore boInfoStore = null;
	   	
	   	IEnterpriseSession boEnterpriseSession = null;
		try{
			boEnterpriseSession = logon(cms);	
			if (null == boEnterpriseSession){
				return false;
			}
			boInfoStore = (IInfoStore)boEnterpriseSession.getService("InfoStore");
			//Retrieve the PluginManager and use it to retrieve User plug-in.
	        boPluginMgr = boInfoStore.getPluginMgr();
	        boUserPlugin = boPluginMgr.getPluginInfo("CrystalEnterprise.User");
	        
	        //Create a new, empty InfoObjects collection.
	        boNewInfoObjects = boInfoStore.newInfoObjectCollection();
	        
	        //Add the plug-in to the collection.  This creates a new InfoObject that represents a new User
	        boNewUser = boNewInfoObjects.add(boUserPlugin);
	        
	    	//Set the name and description of the User.
	        boNewUser.setTitle(sellerID);
	        //cast the InfoObject as an IUser to access the User settings
	        boUserInfo = (IUser)boNewUser;
	        boUserInfo.setFullName(sellerNick);
	        boUserInfo.setConnection(IUser.CONCURRENT);
			boUserInfo.setPasswordExpiryAllowed(false);
			boUserInfo.setPasswordToChangeAtNextLogon(false);
			boUserInfo.setPasswordChangeAllowed(false);
			boUserInfo.setNewPassword("test001");
			//boUserInfo.setEmailAddress(email);
			
			//add to group ReportGroup
			boUserInfo.getGroups().add(getReportGroupId(boInfoStore,reportGroup));
			
			//Commit the changes to the InfoStore.
	        boInfoStore.commit(boNewInfoObjects);
		}
		catch(SDKException e){
			return false;
		}
		finally
	    {	if (null != boEnterpriseSession){
	    		boEnterpriseSession.logoff();
	    	}
	    }
		return true;
	}
	//get seller_nick by seller_id
	public static String getUserNickName(IInfoStore boInfoStore,String seller_id){
		IUser seller = null;
		try{
			seller = (IUser)boInfoStore.query("SELECT * FROM CI_SYSTEMOBJS " +
				"WHERE SI_KIND = 'USER' AND SI_NAME='" + seller_id+ "'").get(0);
			return seller.getFullName();
		}
		catch(SDKException e){
			return e.getMessage();
		}
		
	}
	public static  void main(String[] args){
		BoUser bo = new BoUser("administrator","sinopechrbo00");
		bo.CreateUser("test002", "huangli","1234sap", "10.1.19.141:6400");
		System.out.println("successed");
//		System.out.println(getName(23,23));
		
	}
}
